package com.book.controller;

import com.book.entity.Account;
import com.book.entity.BookInfo;
import com.book.service.AccountService;
import com.book.service.BookInfoService;
import com.book.util.JsonResult;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * @author SuperYu  2024/7/30
 */

@Controller
@Slf4j
public class IndexController {

    AccountService accountService;

    @Resource
    BookInfoService bookInfoService;

    @Autowired
    public void setAccountService(AccountService accountService) {
        this.accountService = accountService;
    }

    @GetMapping("/")
    public String index(Model model) {
        log.info("--- 到 index 页面---");
        // 把首页需要的三大数据，带到页面上；
        List<BookInfo> goodBook = bookInfoService.findGoodBook();
        List<BookInfo> newBook = bookInfoService.findNewBook();
        List<BookInfo> collectionBook = bookInfoService.findCollectionBook();

        model.addAttribute("goodBook",goodBook);
        model.addAttribute("newBook",newBook);
        model.addAttribute("collectionBook",collectionBook);

        return "index";
    }

    @GetMapping("/toLogin")
    public String toLogin() {
        log.info("--- 到 login 页面---");
        return "login";
    }

    @GetMapping("/toDetail/{id}")
    public String toToDetail(@PathVariable int id, Model model) {
        log.info("得到的需要显示详情的id:"+id);
        model.addAttribute("id",id);
        return "book-detail";
    }

    @GetMapping("/toCart")
    public String toCart() {
        return "cart";
    }

    @GetMapping("/test01")
    public String test01(Model model) {
        // 转发：地址档不变：保持原来的请求和响应不变（服务器内部跳转，只发生一次请求）
        // 重定向：地址档会发生变化，会生成新的请求和响应（发生了两次请求）
        model.addAttribute("msg","大家好!");

        return "test01";
    }

    @GetMapping("/test02")
    public String test02(Model model) {
        // 转发：地址档不变：保持原来的请求和响应不变（服务器内部跳转，只发生一次请求）
        // 重定向：地址档会发生变化，会生成新的请求和响应（发生了两次请求）
        model.addAttribute("msg","大家好2!");
        return "redirect:/test01.html";
    }

    @GetMapping("/testList")
    public String test03(Model model) {

        List<Account> list = accountService.findAllAccount();

        model.addAttribute("list",list);

        return "showAccount";
    }

    @GetMapping("/testMap")
    public String test04(Model model) {
        HashMap<String,Account> map = new HashMap();

        map.put("one",new Account("AAA","111"));
        map.put("two",new Account("BBB","222"));
        map.put("three",new Account("CCC","333"));
        map.put("four",new Account("DDD","444"));
        map.put("five",new Account("EEE","555"));


        model.addAttribute("map",map);

        return "showMap";
    }

    @GetMapping("/testValue")
    public String test05(Model model) {

        String v1 = "<font color='red'>大家好,这是第一条信息。</font>";
        String v2 = "<font color='red'>大家好,这是第二条信息。</font>";

        model.addAttribute("v1", v1);
        model.addAttribute("v2", v2);

        return "showValue";
    }

}
